package org.skydiveapplibrary.util;

import java.util.ArrayList;
import java.util.List;

import org.skydiveapplibrary.data.WeightUnit;
import org.skydiveapplibrary.R;

public enum CalculatorType
{
	WingLoading(R.string.calculator_type_wing_loading),
	CanopySize(R.string.calculator_type_canopy_size),
	ExtraWeight(R.string.calculator_type_extra_weight);
	
	private int resourceId;
	
	CalculatorType(int resourceId)
	{
		this.resourceId = resourceId;
	}
	
	public int getResourceId()
	{
		return resourceId; 
	}
	
	public static List<CalculatorType> getItems()
	{
		List<CalculatorType> items = new ArrayList<CalculatorType>();
		for (CalculatorType item: CalculatorType.values())
		{
			items.add(item);
		}
		return items;
	}
	
	public static float calculate(
			CalculatorType type,
			float yourWeight, float equipWeight, WeightUnit weightUnit,
			float canopySizeSqFt, float wingLoadLbsSqFt)
	{
		float result = 0;
		
		// do calculation
		if (type == CalculatorType.WingLoading)
		{
			// calculate
			result = calculateWingLoading(
					yourWeight, equipWeight, weightUnit,
					canopySizeSqFt);
		}
		else if (type == CalculatorType.CanopySize)
		{
			// calculate
			result = calculateCanopySize(
					yourWeight, equipWeight, weightUnit,
					wingLoadLbsSqFt);
		}
		else if (type == CalculatorType.ExtraWeight)
		{
			// calculate
			result = calculateExtraWeight(
					yourWeight, equipWeight, weightUnit,
					canopySizeSqFt, wingLoadLbsSqFt);
		}
		
		return result;
	}
	
	private static float calculateWingLoading(
			float yourWeight, float equipWeight, WeightUnit weightUnit,
			float canopySizeSqFt)
	{
		// protect against div0
		if (canopySizeSqFt == 0)
		{
			return 0;
		}
		
		// add weight, convert to lbs
		float totalWeightLbs =
			WeightUnit.convert(yourWeight + equipWeight, weightUnit, WeightUnit.Pounds);

		return totalWeightLbs/canopySizeSqFt;
	}
	
	private static float calculateCanopySize(
			float yourWeight, float equipWeight, WeightUnit weightUnit,
			float wingLoadLbsSqFt)
	{
		// protect against div0
		if (wingLoadLbsSqFt == 0)
		{
			return 0;
		}
		
		// add weight, convert to lbs
		float totalWeightLbs =
			WeightUnit.convert(yourWeight + equipWeight, weightUnit, WeightUnit.Pounds);
		
		return totalWeightLbs/wingLoadLbsSqFt;
	}
	
	private static float calculateExtraWeight(
			float yourWeight, float equipWeight, WeightUnit weightUnit,
			float canopySizeSqFt, float wingLoadLbsSqFt)
	{
		// add weight, convert to lbs
		float totalWeightLbs =
			WeightUnit.convert(yourWeight + equipWeight, weightUnit, WeightUnit.Pounds);
		
		// get extra weight in lbs
		float extraWeightLbs = (wingLoadLbsSqFt * canopySizeSqFt) - totalWeightLbs;
		
		// convert to weight unit
		return WeightUnit.convert(extraWeightLbs, WeightUnit.Pounds, weightUnit);
	}
}
